package org.ghost.nlp;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.ghost.nlp.algorithm.MaximumMatching;
import org.ghost.nlp.model.DictModel;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author Administrator
 * @date 2021-04-11 12:25
 */
public class Application {
    public static void main(String[] args) throws IOException {
        DictModel dictModel = loadData();

        System.out.println(MaximumMatching.fmm(dictModel, "我爱北京天安门"));
        System.out.println(MaximumMatching.fmm(dictModel, "帝国主义要把我们的地瓜分掉"));

        System.out.println("---------------------------------------------");
        System.out.println(MaximumMatching.rmm(dictModel, "我爱北京天安门"));
        System.out.println(MaximumMatching.rmm(dictModel, "帝国主义要把我们的地瓜分掉"));

        System.out.println("---------------------------------------------");
        System.out.println(MaximumMatching.bmm(dictModel, "我爱北京天安门"));
        System.out.println(MaximumMatching.bmm(dictModel, "帝国主义要把我们的地瓜分掉"));
    }

    private static DictModel loadData() throws IOException {
        DictModel dictModel = new DictModel();

        String filePath = "D:/WorkSpace/nlp/dict";
        dictModel.setDict(
                Files.list(Paths.get(filePath))
                        .filter(Objects::nonNull)
                        .map(it -> {
                            try {
                                return Files.lines(it, StandardCharsets.UTF_8)
                                        .filter(StringUtils::isNotBlank)
                                        .map(f -> {
                                            String[] array = f.split("\t");
                                            if (ArrayUtils.isNotEmpty(array)) {
                                                return array[0];
                                            } else {
                                                return null;
                                            }
                                        }).filter(StringUtils::isNotBlank)
                                        .collect(Collectors.toSet());
                            } catch (IOException e) {
                                e.printStackTrace();
                                return null;
                            }
                        }).filter(Objects::nonNull)
                        .flatMap(Collection::stream)
                        .collect(Collectors.toSet())

        );

        dictModel.setMaxLen(
                dictModel.getDict().stream()
                        .map(String::length)
                        .max((a, b) -> {
                            if (a > b) {
                                return 1;
                            } else if (1 == b) {
                                return 0;
                            } else {
                                return -1;
                            }
                        })
                        .orElse(0)
        );

        return dictModel;
    }
}
